

function t(t) {
  return (t = t.toString())[1] ? t : "0" + t;
}
module.exports = {
  /**************************************2月12前 ***************************************************/
  gonocome: function () {
    return false;
  },
  /*设置模板调整 */
  urlModal: function (content = '', url = '', title = '提示', confirmText = '确定') {
    wx.showModal({
      title,
      content,
      confirmText,
      success: function (t) {
        if (t.confirm) {
          if (url) {
            wx.navigateTo({
              url,
            })
          }
        }
      }
    })
  },
  /*设置toast */
  urlToast: function (content, url, time = 1000, flag = 0, type = 0) {
    wx.showToast({
      title: content,
      // icon: 'succes',
      icon: 'none',
      duration: time,
      mask: true,
      success: function () {
        if (url) {
          if (type == 0) {
            wx.navigateTo({
              url,
            })
          } else {
            wx.switchTab({
              url,
            })
          }

        }
      }
    })
    if (flag) {
      setTimeout(function () {
        wx.navigateBack({});
      }, time)
    }
  },

  /*设置支付 新的封装*/
  orderPay_1: function (ta, pay_type_list = [], t, e, url, can_wei_one, can_wei_two, can_yu_one, can_yu_two) {
    console.log('can_yu_one的值', can_yu_one);
    console.log('can_yu_two的值', can_yu_two);
    var ea = this, aa = pay_type_list;
    1 == aa.length ? (wx.showLoading({
      title: "正在提交",
      mask: !0
    }), 0 == aa[0].payment && ea.WechatPay(ta, t, e, url, can_wei_one, can_wei_two), 3 == aa[0].payment && ea.BalancePay(ta, t, e, url, can_yu_one, can_yu_two)) : wx.showModal({
      title: "提示",
      content: "选择支付方式",
      cancelText: "余额支付",
      confirmText: "微信支付",
      success: function (aa) {
        wx.showLoading({
          title: "正在提交",
          mask: !0
        }), aa.confirm ? ea.WechatPay(ta, t, e, url, can_wei_one, can_wei_two) : aa.cancel && ea.BalancePay(ta, t, e, url, can_yu_one, can_yu_two);
      }
    });
  },
  /*微信支付 */
  WechatPay: function (a, t, e, url, can_wei_one, can_wei_two) {
    if (a.hasOwnProperty('currentTarget')) {
      a = a.currentTarget.dataset.id;
    } else {
      a = a;
    }
    e.request({
      url: t.order.pay_data,
      data: {
        order_id: a,
        pay_type: "WECHAT_PAY"
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (t) {
        0 == t.code && wx.requestPayment({
          timeStamp: t.data.timeStamp,
          nonceStr: t.data.nonceStr,
          package: t.data.package,
          signType: t.data.signType,
          paySign: t.data.paySign,
          success: function (t) { },
          fail: function (t) { },
          complete: function (t) {
            console.log('支付情况?', t);
            "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? wx.redirectTo({
              url: url + "?" + can_wei_one
            }) : wx.showModal({
              title: "提示",
              content: "订单尚未支付",
              showCancel: !1,
              confirmText: "确认",
              success: function (t) {
                t.confirm && wx.redirectTo({
                  url: url + "?" + can_wei_two
                });
              }
            });
          }
        }), 1 == t.code && wx.showToast({
          title: t.msg,
          // image: "/images/icon-warning.png"
        });
      }
    });
  },
  /*余额支付 */
  BalancePay: function (a, t, e, url, can_yu_one, can_yu_two) {
    console.log('余额支付can_yu_one', can_yu_one);
    console.log('余额支付can_yu_two', can_yu_two);
    if (a.hasOwnProperty('currentTarget')) {
      a = a.currentTarget.dataset.id;
    } else {
      a = a;
    }
    console.log('a的值', a);
    e.request({
      url: t.order.pay_data,
      data: {
        order_id: a,
        pay_type: "BALANCE_PAY"
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (t) {

        0 == t.code && wx.redirectTo({
          url: url + '?' + can_yu_one
        }), 1 == t.code && wx.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          confirmText: "确认",
          success: function (t) {
            t.confirm && wx.redirectTo({
              url: url + "?" + can_yu_two
            });
          }
        });
      }
    });
  },
  /*封装上传图片 */
  uploadimg_one(tmp_file, new_name, ea = this, s) {
    let imgsrc = '';
    //http://tmp>0为开发者工具上传，wxfile://为手机上传文件
    if (tmp_file.indexOf('http://tmp') >= 0 || tmp_file.indexOf("wxfile://") >= 0) {
      console.log('执行tmp');
      wx.uploadFile({
        url: s.default.upload_image,
        name: "image",
        formData: {
          'user': 'text'
        },
        filePath: tmp_file,
        success: function (t) {
          if (t.data) {
            imgsrc = JSON.parse(t.data).data.url;
            ea.setData({
              [new_name]: imgsrc
            })
          }
        }
      });
    } else {
      console.log('执行现在');
      ea.setData({
        [new_name]: tmp_file
      })
    }
  },
  /*封装上传所有图片 3月26日*/
  uploadimg_all(tmp_file, new_name, ea = this, s) {
    let imgsrc = '', attr = new Array();
    //http://tmp>0为开发者工具上传，wxfile://为手机上传文件
    for (let i = 0; i < tmp_file.length; i++) {
      if (tmp_file[i].indexOf('http://tmp') >= 0 || tmp_file[i].indexOf("wxfile://") >= 0) {
        console.log('执行tmp');
        wx.uploadFile({
          url: s.default.upload_image,
          name: "image",
          formData: {
            'user': 'text'
          },
          filePath: tmp_file[i],
          success: function (t) {
            if (t.data) {
              imgsrc = JSON.parse(t.data).data.url;
              attr.push(imgsrc);
              ea.setData({
                [new_name]: attr
              })
            }
          }
        });
      } else {
        console.log('执行现在');
        ea.setData({
          [new_name]: attr
        })
      }
    } (s)
  },
  /*封装上传所有图片 3月26日*/
  /*数组去重操作 */
  uniq(array) {
    var temp = []; //一个新的临时数组
    for (var i = 0; i < array.length; i++) {
      if (temp.indexOf(array[i]) == -1 && array[i] != 'undefined') {
        temp.push(parseInt(array[i]));
      }
    }
    return JSON.stringify(temp);
  },
  /*延时跳转 */
  settime_go(url, type = 1, time = 500) {
    setTimeout(function () {
      if (type == 1) {
        /*保留当前页面,跳转某个页面 */
        wx.navigateTo({
          url,
        })
      } else if (type == 2) {
        /*关闭当前页面，跳转到某个页面 */
        wx.redirectTo({
          url,
        })
      } else if (type == 3) {
        /*关闭所有页面，跳转到某个页面 */
        wx.reLaunch({
          url,
        })
      }
    }, time)
  },
  /*封装登录进来绑定上下级 带type(4.9)*/
  login_brand(options, id = 'id', ia = this, tihuan = '', type = '', a = getApp(), ut = require("./utils.js")) {
    var l = 0,
      g = options.user_id, /*带分享链接过来 */
      m = decodeURIComponent(options.scene);  /*图片邀请码分享过来 */
    /*如果有user_id */
    if (void 0 != g) {
      l = g;
    } else if (void 0 != m) {
      var f = ut.scene_decode(m);
      console.log('f的值', f);
      if (f[id]) {
        // l = m  修改
        f.uid && f[id] ? (l = f.uid, options.id = f[id]) : options.id = f[id]; /*id统一化,如为bid，后端图片参数也为bid */
        console.log('options.id的值', options.id);
        if (tihuan) {
          ia.setData({
            [tihuan]: f[id]
          })
        }
        console.log('type的值', type);
        /**是否有类型type */
        if (type) {
          ia.setData({
            [type]: f[type] || 1
          })
        }
        ia.setData({
          [id]: f[id],
        });
      }
    }
    a.loginBindParent({ parent_id: l });
  },
  /***********************************************封装删除导航方法(因为ea.data不在当前js,需要到其他js使用)*/
  del_cate_one(nowidx, catId = '', ea = this) {
    ea.data[catId].splice(nowidx, 1);    //删除当前索引的内容，这样就能删除view了
    ea.setData({ [catId]: ea.data[catId] });
  },
  /*封装砍价商品处理*/
  handle_br(bid, a, s) {
    let goods_info;
    /*当前页处理逻辑 */
    var e = this;
    wx.showNavigationBarLoading(), a.request({
      url: s.bargain.doing,
      data: {
        goods_id: bid
      },
      success: function (s) {
        wx.hideNavigationBarLoading();
        goods_info = s.goods_info;
        if (0 == s.code) {
          wx.showModal({
            content: '砍价成功，是否去查看',
            cancelText: '取消',
            confirmText: '确定',
            success: function (res) {
              if (res.confirm) {
                /*如果有下单信息 */
                if (goods_info) {
                  wx.navigateTo({
                    url: '/pages/kill_goods_page/kill_goods_page?bid=' + goods_info.br_id,
                  })
                  /*否则跳砍价信息 */
                } else {
                  wx.navigateTo({
                    url: '/pages/bargain_order_list/bargain_order_list',
                  })
                }
              }
            }
          })
        } else if (1 == s.code) {
          wx.showModal({
            content: s.msg,
          })
        } else {
          wx.showModal({
            content: '预购超次数或失败,请重试!',
          })
        }
      }
    });
  },
  /*封装拼团商品处理 */
  /*拼团提交表单 */
  submit_group: function (t, a) {
    var ea = this, i = "GROUP_BUY" == t;
    if (!ea.data.show_attr_picker || i != ea.data.groupNum) return ea.setData({
      show_attr_picker: !0,
      groupNum: i
    }), !0;
    if (ea.data.form.number > ea.data.ptGoods.num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      // image: "/images/icon-warning.png"
    }), !0;
    var o = ea.data.ptAttr_group_list, r = [];
    for (var s in o) {
      var n = !1;
      for (var d in o[s].attr_list) if (o[s].attr_list[d].checked) {
        n = {
          attr_id: o[s].attr_list[d].attr_id,
          attr_name: o[s].attr_list[d].attr_name
        };
        break;
      }
      if (!n) return wx.showToast({
        title: "请选择" + o[s].attr_group_name,
        // image: "/images/icon-warning.png"
      }), !0;
      r.push({
        attr_group_id: o[s].attr_group_id,
        attr_group_name: o[s].attr_group_name,
        attr_id: n.attr_id,
        attr_name: n.attr_name
      });
    }
    ea.setData({
      show_attr_picker: !1
    });
    var c = 0;
    // console.log(JSON.stringify({
    //   goods_id: ea.data.ptGoods.id,
    //   attr: r,
    //   num: ea.data.form.number,
    //   type: t,
    //   deliver_type: ea.data.ptGoods.type,
    //   group_id: a,
    //   parent_id: c
    // }));
    ea.data.oid && (t = "GROUP_BUY_C", c = ea.data.oid), wx.redirectTo({
      url: "/pages/pt_order_settlement/pt_order_settlement?goods_info=" + JSON.stringify({
        goods_id: ea.data.ptGoods.id,
        attr: r,
        num: ea.data.form.number,
        type: t,
        deliver_type: ea.data.ptGoods.type,
        group_id: a,
        parent_id: c
      })
    });
  },
  /**************************************2月12前 ****************************************************/
  /**************************************2月13开始 ***************************************************/
  /*数字转数组函数 */
  number_attr: function (attr, key) {
    for (var iaa = 0; iaa < attr[key]; iaa++) {
      attr[key + '_attrs'].push(0);
    } (iaa);
  },

  /**************************************2月13结束 ***************************************************/

  /*******************3月27最后导航 */
  goDh: function (e, type = 0, longitude = 0, latitude = 0, name = '默认') {
    var new_longitude, new_latitude, new_name;
    if (type == 0) {
      var current = e.currentTarget.dataset,
        new_longitude = Number(current.longitude),
        new_latitude = Number(current.latitude),
        new_name = current.name;
    } else {
      new_longitude = longitude;
      new_latitude = latitude;
      new_name = name;
    }
    wx.openLocation({
      type: 'gcj02',
      latitude: Number(new_latitude),
      longitude: Number(new_longitude),
      name: new_name,
      scale: 28
    })
  },
  /*******************6月10日 16:44 */

  cremb_scene(options, ia = this, tihuan = '', type = '', a = getApp(), ut = require("./utils.js")) {
    var l = 0,
    m = decodeURIComponent(options.scene);  /*图片邀请码分享过来 */
    console.log('m的值',m)
    /*如果有user_id */
    if (void 0 != m) {
      var f = ut.scene_decode(m);
      ia.setData({
        red_page_id: f['red_page_id'],
      })
    }
  },


  cremb_scene_can_ms(options, ia = this,id='id',tihuan = 'id', type = '', a = getApp(), ut = require("./utils.js")) {
    var l = 0,
      m = decodeURIComponent(options.scene);  /*图片邀请码分享过来 */
    console.log('m的值', m)
    /*如果有user_id */
    if (void 0 != m) {
      var f = ut.scene_decode(m);
      console.log('f的值',f);
      ia.setData({
        id: f['id'],
        time: f['time'],
        status: f['status'],
      })
    }
  },

  cremb_scene_can_pro(options, ia = this, id = 'id', tihuan = 'id', type = '', a = getApp(), ut = require("./utils.js")) {
    var l = 0,
      m = decodeURIComponent(options.scene);  /*图片邀请码分享过来 */
    console.log('m的值', m)
    /*如果有user_id */
    if (void 0 != m) {
      var f = ut.scene_decode(m);
      console.log('f的值', f['product_id']);
      console.log('id的值',id);
      ia.setData({
        id: f['product_id'],
      })
    }
  },

  getTime(date){
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ?'0'+(date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ?'0' + date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ?'0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ?'0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    var ztime = Y + M + D + h + m + s
    return ztime;
  }
  
    
};